package com.zcxy.fast.controller;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import com.zcxy.fast.bean.Response;
import com.zcxy.fast.entity.SysAttachment;
import com.zcxy.fast.util.MonioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zcc
 * @version 1.0.0
 * @ClassName MinioController.java
 * @Description TODO
 * @createTime 2020年06月30日 10:46:00
 */
@Slf4j
@RestController
@RequestMapping("/sys")
@Api(tags = "<font  color='red'>文件接口</font>")
public class MinioController {


    private static final String MINIO_BUCKET = "plmsys";


    @GetMapping("/list/{fileUuid}")
    @ApiOperation(value = "文件列表", notes = "文件列表")
    public Response<List<SysAttachment>> list(@PathVariable("fileUuid") String fileUuid) throws Exception {
        return Response.ok(MonioUtil.getSysattachmentsByUUid(fileUuid));
    }

    @PostMapping("/batchUpload")
    @ApiOperation(value = "批量上传接口", notes = "批量上传接口")
    public Response<List<SysAttachment>> batchUpload(@RequestParam(name = "file", required = false) MultipartFile[] file, @RequestParam(name = "fileUuid", required = false) String fileUuid) {
        Response res = new Response();
        res.setCode(500);
        //  insertOrFindbucket(MINIO_BUCKET + "/" + bussnessId);
        if (file == null || file.length == 0) {
            res.setMessage("上传文件不能为空");
            return res;
        }
        //
        List<SysAttachment> sysAttachments = MonioUtil.batchUpload(file, fileUuid);
        res.setResult(sysAttachments);
        return res;
    }


    @PostMapping("/upload")
    @ApiOperation(value = "单个上传接口", notes = "单个上传接口")
    public Response<SysAttachment> upload(@RequestParam(name = "file", required = false) MultipartFile file, @RequestParam(name = "fileUuid", required = false) String fileUuid) {
        Response res = new Response();
        res.setCode(500);
        //  insertOrFindbucket(MINIO_BUCKET + "/" + bussnessId);
        if (file == null) {
            res.setMessage("上传文件不能为空");
            return res;
        }
        MultipartFile[] multipartFiles = new MultipartFile[1];
        multipartFiles[0] = file;
        //
        List<SysAttachment> sysAttachments = MonioUtil.batchUpload(multipartFiles, fileUuid);
        if (sysAttachments.size() > 0) {
            res.setResult(sysAttachments.get(0));
        }
        res.setCode(200);
        return res;
    }

    @GetMapping("/download/{fileName}")
    @ApiOperation(value = "下载", notes = "下载")
    public void download(HttpServletResponse response, @PathVariable("fileName") String fileName) {
        DES des = SecureUtil.des(MonioUtil.SALT.getBytes());
        String encrypt = des.encryptHex(fileName);

        MonioUtil.download(response, encrypt);
    }

    /*public static void main(String[] args) {
        DES des = SecureUtil.des(MonioUtil.SALT.getBytes());
        String encrypt = des.encryptHex("20210121055500-sm-nodata.png");
        System.out.println(encrypt);
    }*/

}
